package com.sundog.sundroid;



import java.util.Map;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.sundog.sundroid.google.GoogleService;
/**
 * Class for resolving votes
 * 
 * @author Matt
 *
 */
public class ResolveVote extends Activity implements OnClickListener {
	
	/** The resolved vote */
	public String vote;
	/** Message filter */
	PacketFilter filter = new PacketTypeFilter(Message.class);
	
	/** Create a packet listener to process messages */
	PacketListener myListener = new PacketListener() {
		
		public void processPacket(Packet packet) {
			Message msg = (Message) packet;			
       }
	};
    
	/** Sets up XMPP connection parameters */
	private final ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 80, "jabber.org"); 
	/** Establishes XMPP connection using the connection configuration */
	private final XMPPConnection connection = new XMPPConnection(connConfig);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resolvevote);
		
		// Connect to the XMPP server
		connectToServer();
		
		// Retrieve resolved vote
		vote = this.getIntent().getExtras().getString("resolvedVote");
		Log.d("RES", this.getIntent().getExtras().getString("resolvedVote"));
		
		// Send vote to sundroiddemo and disconnect from server
		Message message = new Message();
		message.setType(Message.Type.headline);
		message.setTo("sundroiddemo@gmail.com");
		message.setBody("Resolve:" + vote);
	    connection.sendPacket(message);
	    connection.disconnect();
	    
		TextView details = (TextView) this.findViewById(R.id.user);
		details.setText("" + vote);
		
		// Set up click listener
		View startButton = this.findViewById(R.id.nextstory_button);
	    startButton.setOnClickListener(this);	    
	}
	
	/**
	 * Process button clicks
	 */
	public void onClick(View v) {
    	switch (v.getId()) {
    	
    	case R.id.nextstory_button:
    		 		
    		Intent i = new Intent(this, scrumMaster.class);
    		startActivity(i);
    		break;
    	}
	}
	
	/**
	 * Connect to the XMPP server
	 */
    private void connectToServer() {
    	try {	
    		// Connect to the server if not already connected and log in with user name and password from previous activity
    		if(!connection.isConnected()) {
    			Log.d("***", "Admin Connect results...");
    			connection.connect();
    			connection.login("demosundog@gmail.com", "sundog101");
    			Log.d("***", "Admin Connected results");
    		}
    	} catch (XMPPException e) {
    		Log.d("***", "XMPPException" + e.getMessage());
    	}
    	
    	// Add a packet listener to the connection. The parameters were created above
    	connection.addPacketListener(myListener, filter);
    }
}
